package com.jh.paymentchannel.service;

import cn.jh.common.utils.CommonConstants;
import cn.jh.common.utils.TokenUtil;
import com.jh.paymentchannel.business.RealnameImgBusiness;
import com.jh.paymentchannel.pojo.RealNameImg;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@EnableAutoConfiguration
public class RealnameImgService {

    private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(RealnameImgService.class);

    @Autowired
    private RealnameImgBusiness realnameImgBusiness;

    /**
     * 聚合图片识别
     * param:image base64
     * param:side 类型0:1
     * return:realname
     * return:idcard
     * auth:sxl
     */
    @RequestMapping(method = RequestMethod.POST, value = "/v1.0/paymentchannel/realname/img/{token}")
    public @ResponseBody
    Object realnameAuth(HttpServletRequest request,
                        @RequestParam(value = "image") String image,
                        @RequestParam(value = "side") String side,
                        @PathVariable("token") String token) {

        //token!=userid
        Map<String, Object> map = new HashMap<>();
        long userId;
        try {
            userId = TokenUtil.getUserId(token);
        } catch (Exception e) {
            map.put(CommonConstants.RESP_CODE, CommonConstants.ERROR_TOKEN);
            map.put(CommonConstants.RESP_MESSAGE, "token无效");
            return map;
        }

        //已经有了个人信息
        HashMap<Object, Object> realNameMap = realnameImgBusiness.findResultByUserid(userId);
        String flag = (String) realNameMap.get("success");
        if (flag != null && "0".equals(flag)) {
            String name = (String) realNameMap.get("realname");
            String card = (String) realNameMap.get("idcard");
            HashMap<Object, Object> result = new HashMap<>();
            result.put("name",name);
            result.put("card",card);
            map.put(CommonConstants.RESP_CODE, CommonConstants.SUCCESS);
            map.put(CommonConstants.RESULT, result);
            map.put(CommonConstants.RESP_MESSAGE, "已有个人信息");
            return map;
        }

        //操作频繁
        RealNameImg realNameImg = realnameImgBusiness.findRealNamesImgByUserid(userId);
        if (realNameImg != null && realNameImg.getAuthTime() != null) {
            long starttime = realNameImg.getAuthTime().getTime();
            long endtime = System.currentTimeMillis();
            starttime = starttime + 60 * 10 * 1000;
            if (starttime > endtime) {
                map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
                map.put(CommonConstants.RESP_MESSAGE, "操作过于频繁，请稍后再试!");
                return map;
            }
        }

        map.put(CommonConstants.RESP_CODE, CommonConstants.SUCCESS);
        map.put(CommonConstants.RESULT, realnameImgBusiness.realNameImg(userId, side, image));
        map.put(CommonConstants.RESP_MESSAGE, "接口返回成功");
        return map;
    }

   /* *//**
     * @Description: 根据图片地址转换为base64编码字符串
     * @Author:
     * @CreateTime:
     * @return
     *//*
    public static String getImageStr(String imgFile) {
        InputStream inputStream = null;
        byte[] data = null;
        try {
            inputStream = new FileInputStream(imgFile);
            data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 加密
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }

    public static void main(String[] args) {
       String strImg= getImageStr("C:/123/1.jpg");
        System.out.println(strImg);
    }*/
}